from sanic import Blueprint, response

from models.explore import Explore, Explore_Reason, Explore_Solve, Explore_Evaluation

exp = Blueprint("explore")


# 月龄探索列表接口
@exp.route("/grow/explore/age_list")
async def explore(request):
    data = await Explore.all().order_by("explore_order")
    data_list = []
    for d in data:
        a = {"id": d.id, "name": d.name, "introduction": d.introduction}
        data_list.append(a)
    return response.json({"code": 200, "message": "", "data": data_list})


# 探索详情页接口
@exp.route("/grow/explore/detail/<explore_id>")
async def explore_detail(request, explore_id):
    data = await Explore.get(id=explore_id)
    # 原因数据
    reason = await Explore_Reason.filter(explore_id=explore_id)
    reason_list = [{"id": r.reason_id, "reason_content": r.reason_content} for r in reason]
    # 解决思路数据
    solve = await Explore_Solve.filter(explore_id=explore_id)
    solve_list = [{"id": s.solve_id, "solve_content": s.solve_content} for s in solve]
    # 测评数据
    evaluation = await Explore_Evaluation.filter(explore_id=explore_id)
    evaluation_list = [{"gauge_id": e.gauge_id, "recommendation": e.recommendation} for e in evaluation]
    return response.json({"code": 200, "message": "",
                          "explore": {"id": data.id, "name": data.name, "introduction": data.introduction,
                                      "cover": data.cover, "audio": data.audio, "passage": data.passage,
                                      "update_time": data.update_time.strftime('%Y-%m-%d %H:%M:%S')},
                          "reason": reason_list, "solve": solve_list, "evaluation": evaluation_list})


